<template>
  <el-card>
    <el-table :data="roleList" style="width: 100%" row-key="id">
      <el-table-column prop="name" label="菜单名称" width="width" sortable="true"></el-table-column>
      <el-table-column prop="code" label="权限标识" width="width" sortable="true"></el-table-column>
      <el-table-column prop="description" label="描述" width="width"></el-table-column>
      <el-table-column prop="prop" label="操作" width="300px">
        <template slot-scope="{row}">
          <a v-if="row.type!=2" @click="showAddPermission(row.id,row.type)">添加</a>
          <a @click="showUpdatePermission(row.id)">编辑</a>
          <a class="del" @click="delPermission(row.id)">删除</a>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      :title="title"
      :visible.sync="dialogFormVisible"
      width="500px"
      :before-close="cancel"
    >
      <el-form :model="form">
        <el-form-item label="名称" label-width="100px">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="标识" label-width="100px">
          <el-input v-model="form.code" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="描述" label-width="100px">
          <el-input v-model="form.description" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" label-width="100px">
          <el-switch v-model="form.enVisible"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addPermission">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>
<script>
export default {
  data() {
    return {
      roleList: [],
      dialogFormVisible: false,
      form: {
        enVisible: 0,
        name: "",
        code: "",
        description: "",
        type: "",
        pid: ""
      }
    };
  },
  computed: {
    title() {
      return this.form.id ? "修改权限" : "添加权限";
    }
  },
  mounted() {
    this.getPermissionsList();
  },
  methods: {
    async getPermissionsList() {
      let list = await this.$api.role.getPermissionList();
      this.roleList = this.treeOfList(list, "0");
    },

    //数据树遍历
    treeOfList(list, flag) {
      let arr = [];
      list.forEach(element => {
        if (element.pid == flag) {
          let children = this.treeOfList(list, element.id);
          if (children.length) {
            element.children = children;
          }
          arr.push(element);
        }
      });
      return arr;
    },

    //展示添加权限
    showAddPermission(pid = "0", type = 0) {
      this.dialogFormVisible = true;
      this.form.pid = pid;
      this.form.type = type + 1;
    },

    //添加或更新权限
    async addPermission() {
      this.form.enVisible = this.form.enVisible ? "1" : "0";
      if (this.form.id) {
        await this.$api.role.setPermissionById(this.form.id, this.form);
        this.$message.success("更新成功!");
      } else {
        await this.$api.role.addlPermission(this.form);
        this.$message.success("添加成功!");
      }
      this.dialogFormVisible = false;
      this.getPermissionsList();
    },

    //添加取消
    cancel() {
      this.form = {};
      this.dialogFormVisible = false;
    },

    delPermission(id) {
      this.$confirm("确认删除吗?", {
        cancelButtonText: "取消",
        confirmButtonText: "确认"
      })
        .then(() => {
          return this.$api.role.delPermissionById(id);
        })
        .then(() => {
          this.$message.success("删除成功!");
          this.getPermissionsList();
        });
    },

    //编辑
    showUpdatePermission(id) {
      this.$api.role.getPermissionById(id).then(res => {
        this.form = res;
        this.form.enVisible = this.form.enVisible == "1" ? true : false;
        this.dialogFormVisible = true;
      });
    }
  }
};
</script>

<style>
.img {
  width: 100px;
  height: 100px;
  border-radius: 50px;
}
a {
  font-size: 12px;
  color: blue;
  margin-right: 20px;
}
.del {
  color: darkgrey !important;
}
</style>